import { createSlice } from '@reduxjs/toolkit'
import { loginType } from '@/types'

const initialState: loginType = {
  username: '',
  password: '',
}

export const userSlice = createSlice({
  name: 'user',
  initialState,
  reducers: {
    loginReducer: (_state, action) => {
      return action.payload
    },
    logoutReducer: () => {
      localStorage.removeItem('token')
      window.location.href = '/login'
      return initialState
    },
  },
})

export const { loginReducer, logoutReducer } = userSlice.actions
export default userSlice.reducer
